#pragma once
#include "stdafx.h"
#include <windows.h>
#include <process.h>
#include <stdlib.h>
using namespace System::Threading;
using namespace System;


HINSTANCE hinstLib;

class CallerIDManshorSimin
{		
	static void LineHangUp(int DeviceIndex, int LineNumber, int LType)
	{
		//Your Code Here...
	}
	static void LineHangDown(int DeviceIndex, int LineNumber, int LTime)
	{
		//Your Code Here...
	}
	static void LinePhoneDigit(int DeviceIndex, int LineNumber, char LDigit)
	{
		//Your Code Here...
	}
	static void LineRing(int DeviceIndex, int LineNumber, int LCountRing)
	{
		//Your Code Here...
	}
	static void LOfflineCallerId(int DeviceIndex, char CallerID[], int TypeOfCallerID)
	{
	}

	static void LMissCall(int DeviceIndex, int LineNumber, int CountRing)
	{
		//Your Code Here...
	}
	static void LDisConnect(int DeviceIndex, int LineNumber)
	{
		//Your Code Here...
	}
	static void LineError(int DeviceIndex, int LineNumber, int ErrorCode)
	{
		//Your Code Here...
	}
	static void DeviceDisConnect(int DeviceIndex, int DeviceSerial)
	{
		//Your Code Here...
	}
	static void DeviceConnect(int DeviceIndex, int DeviceSerial, int CountLine)
	{
		//Your Code Here...
	}
	static void LCallerID(int DeviceIndex, int LineNumber, char CallerID[])
	{		
		//printf("%s\n", CallerID);
		//Your Code Here...
	}
	static void OutStringData(char DataString[])
	{
		printf("%s\n", DataString);
		//Your Code Here...
	}

	//-----------------  Your Code[End]  -----------------

	//-----------------  DON`T CHANGE THIS CODE[End]  -----------------
	static DWORD WINAPI myThread(LPVOID lpParameter)
	{
		Byte DataArray[65];
		char str[40];
		typedef int(__cdecl *MYPROC1)(Byte* PointerOfArray);
		MYPROC1 GetNextCMDString;
		MYPROC1 GetNextCMD;
		GetNextCMD = (MYPROC1)GetProcAddress(hinstLib, "GetNextCMD");
		GetNextCMDString = (MYPROC1)GetProcAddress(hinstLib, "GetNextCMDString");
		while (1)
		{			
			GetNextCMD(DataArray);			
			if (DataArray[1] != 0)
			{
				switch (DataArray[1])
				{
				case 1:
					//Don`t use.
					break;

				case 2: //Caller ID					
					int i;
					for (i = 0; i < DataArray[4]; i++)
					{								
						str[i] = (DataArray[5 + i]);
					}			
					str[i] = 0;
					LCallerID(DataArray[2], DataArray[3], str);
					break;

				case 3://Hang Down
					if (DataArray[2] == 1)
					{
						LMissCall(DataArray[3], DataArray[4], DataArray[5]);
					}
					if (DataArray[2] == 2)
					{
						LineHangDown(DataArray[3], DataArray[4], DataArray[5] * 256 + DataArray[5]);
					}
					break;

				case 4://Hang Up
					LineHangUp(DataArray[2], DataArray[3], DataArray[4]);
					break;

				case 5://Dialed Number
					LinePhoneDigit(DataArray[2], DataArray[3], (char)DataArray[4]);
					break;

				case 6://Line Ring
					LineRing(DataArray[2], DataArray[3], DataArray[4]);
					break;

				case 7://Line DisConnect
					LDisConnect(DataArray[2], DataArray[3]);
					break;

				case 8:
					//Don`t use.
					break;

				case 9://Device Connect
					DeviceConnect(DataArray[2], DataArray[3], DataArray[4]);
					break;

				case 10://Device Disconnect
					DeviceDisConnect(DataArray[2], DataArray[3]);
					break;

				case 11:
					//Don`t use.
					break;

				case 12:
					LineError(DataArray[2], DataArray[3], DataArray[4]);
					//Don`t use.
					break;

				case 64:										
					for (i = 0; i < DataArray[3]; i++)
					{
						str[i] = (DataArray[4 + i]);
					}
					str[i] = 0;
					LOfflineCallerId(DataArray[2], str, DataArray[4]);
					break;

				default:
					break;
				}
				DataArray[1] = 0;
			}
			GetNextCMDString(DataArray);
			if (DataArray[1] != 0)
			{				
				int i;
				for (i = 0; i < DataArray[1]; i++)
				{
					str[i] = (char)(DataArray[2 + i]);
				}
				str[i] = 0;
				OutStringData(str);
				DataArray[1] = 0;
			}

			Sleep(100);
		}
		return 0;
	}

	HANDLE Handle_Of_Thread= 0;
	typedef int(__cdecl *MYPROC)();
	typedef int(__cdecl *MYPROC1)(char* PointerOfArray);
	MYPROC SetInit;
	MYPROC SetLanguageCPlusPlus;
public:
	CallerIDManshorSimin()
	{
		
	}

	~CallerIDManshorSimin()
	{
	}
public:
	void StartGetData();
};

void CallerIDManshorSimin::StartGetData()
{	
	hinstLib = LoadLibrary("DllTechnoCaller.dll");
	int t1 = 0;
	if (hinstLib != NULL)
	{
		SetInit = (MYPROC)GetProcAddress(hinstLib, "SetInit");
		SetLanguageCPlusPlus = (MYPROC)GetProcAddress(hinstLib, "SetLanguageCPlusPlus");
		if (NULL != SetInit)
		{
			SetLanguageCPlusPlus;
			t1 = (SetInit)();
			if (t1 == 1)
			{
				DWORD myThreadID;
				DWORD myCounter;
				HANDLE myHandle = CreateThread(0, 0, myThread, &myCounter, 0, &myThreadID);
			}
		}
	}
}
